package com.card.game;

import com.card.common.MessageComm;
import com.card.user.User;
import com.xiaoao.net.json.message.JsonMessage;

public abstract class BasePlayer {
	public enum UserState{
		None,
		Ready,
		Runnig,//正在进行
		WaitBet,
		FapaiComplete,
		Bet,
		End,//比赛结束
	}
	protected int tableIndex;//桌位
	protected String userId;
	public String img;
	public String nickName;
	public int sex;
	private String ip;
	private double gps;
	private double gps2;
	private int score = 0;
	private UserState state = UserState.None;
	private boolean isOnline = true;
	protected transient PlayerInfo playerInfo;
	
	protected transient User user;
	
	public BasePlayer(User user) {
		this.user = user;
		this.userId = user.getPrimaryKey();
		this.img = user.userExtra().getImg();
		this.nickName = user.userExtra().getNickName();
		this.sex = user.userExtra().getSex();
		this.ip = user.getIp();
		this.gps = user.getGps();
		this.gps2 = user.getGps2();
		this.playerInfo = new PlayerInfo(this);
	}
	public String getUserId() {
		return userId;
	}
	public void setUser(User user) {
		this.user = user;
		this.ip = user.getIp();
	}
	public int getTableIndex() {
		return tableIndex;
	}
	public void setTableIndex(int tableIndex) {
		this.tableIndex = tableIndex;
	}
	public String getIp() {
		return ip;
	}
	public User getUser() {
		return user;
	}
	
	
	public double getGps() {
		return gps;
	}
	public double getGps2() {
		return gps2;
	}
	public void clear() {
		if(user != null) user.setRoom(null); 
		this.user = null;
	}
	
	public void endGame() {
		this.state = UserState.None;
	}
	
	public UserState getState() {
		return state;
	}
	public void setState(UserState state) {
		this.state = state;
	}
	public PlayerInfo getPlayerInfo() {
		return playerInfo;
	}
	public int getScore() {
		return score;
	}
	public void addScore(int score) {
		this.score += score;
	}
	public void changeOnline(boolean isOnline) {
		this.isOnline = isOnline;
		JsonMessage message = JsonMessage.createMessage(MessageComm.S2C_Player_Online_Change);
		message.addProperty("userId", userId);
		message.addProperty("isOnline", isOnline);                                    
		user.getRoom().broadcast(message);
	}
	public boolean isOnline() {
		return isOnline && user != null && user.isOnline();
	}
}
